package model;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;


public class CityMap {
	
	public enum CELL_TYPE {
		  ROAD,
		  PALACE   
	}
	
	private List<Car> cars;
	
	
//	private Car carToAdd = new Car();
//	private boolean adding = false;
//
//	
//	public synchronized void setCarToAddStart(Street s, int index){
//		carToAdd.setCurPos(s);
//		carToAdd.setCurPosIndex(index);
//	}
//	
//	public synchronized void setCarToAddEnd(Street s, int index){
//		carToAdd.setDestPos(s);
//		carToAdd.setDestPosIndex(index);
//		adding = true;
//	}
//	
//	public synchronized Car getCarToAdd(){
//		if(adding){
//			adding = false;
//			return carToAdd;
//		}
//		else
//			return null;
//	}
	

	public CityMap() {
		cars = new ArrayList<Car>();
	}
	
	boolean isCrossroads(final Point p){
		return false;
		
	}
	
	public synchronized List<Car> getCars() {
		return cars;
	}
		
	public int getnCells() {
		return Config.numPalaces*Config.nCellsPalace + (Config.numPalaces+1)*Config.nCellsRoad;
	}
	
	public CELL_TYPE getTerrain(final Point key){
		final int i = key.x;
		final int j = key.y;
	    if(i % (Config.nCellsPalace+Config.nCellsRoad) >= Config.nCellsRoad && j % (Config.nCellsPalace+Config.nCellsRoad) >= Config.nCellsRoad)
	    	return CELL_TYPE.PALACE;
	    else
	    	return CELL_TYPE.ROAD;
	}


	public int carsBetween(final Crossroads p1, final Crossroads p2) {
		int cont = 0;
		for(final Car c: cars)
			if(c.getCurPos().getStart().equals(p1) && c.getCurPos().getEnd().equals(p2))
				cont++;
		return cont;
		
	}

	public boolean freeRoad(final Street curPos, final int curPosIndex) {
		for(final Car c: getCars())
			if(c.getCurPos().equals(curPos) && c.getCurPosIndex() == curPosIndex)
				return false;
		return true;
	}
}
